package cn.xiaopengstack.domain.activity.service.armory;

import cn.xiaopengstack.domain.activity.model.entity.RaffleActivityEntity;
import cn.xiaopengstack.domain.activity.model.entity.RaffleActivitySkuEntity;
import cn.xiaopengstack.domain.activity.model.valobj.ActivitySkuStockKeyVO;
import cn.xiaopengstack.domain.activity.repository.IRaffleActivityRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author jiangyangang
 */
@Component
@Slf4j
public class ActivityArmory implements IActivityArmory, IActivityDispatch{

    @Resource
    private IRaffleActivityRepository activityRepository;

    @Override
    public boolean assembleActivitySku(Long sku) {
        // 活动sku预热
        RaffleActivitySkuEntity skuEntity = activityRepository.queryActivitySku(sku);

        // 活动sku库存预热
        activityRepository.cacheActivitySkuStockCount(sku, skuEntity.getStockCountSurplus());

        // 活动预热
        activityRepository.queryRaffleActivityByActivityId(skuEntity.getActivityId());

        // 活动次数配置预热
        activityRepository.queryRaffleActivityCountByActivityCountId(skuEntity.getActivityCountId());

        return true;
    }

    @Override
    public boolean assembleActivitySkuByActivityId(Long activityId) {
        List<RaffleActivitySkuEntity> raffleActivitySkuEntities = activityRepository.queryActivitySkuByActivityId(activityId);
        for (RaffleActivitySkuEntity skuEntity : raffleActivitySkuEntities) {
            // 活动sku库存预热
            activityRepository.cacheActivitySkuStockCount(skuEntity.getSku(), skuEntity.getStockCountSurplus());
            // 活动次数配置预热
            activityRepository.queryRaffleActivityCountByActivityCountId(skuEntity.getActivityCountId());
        }

        // 活动预热
        activityRepository.queryRaffleActivityByActivityId(activityId);

        return true;
    }

    @Override
    public boolean subtractionActivitySkuStock(ActivitySkuStockKeyVO skuStockKeyVO) {
        return activityRepository.subtractionActivitySkuStock(skuStockKeyVO);
    }
}
